<template>
	<text class="lang-wrapper" v-if="keywordsSet && tag == ''" :class="(isRight ? 'lang-right' : '') + ' ' + (isLeft ? 'lang-left' : '')">{{keywordsSet}}</text>
	<cover-view class="lang-wrapper" v-else-if="keywordsSet" :class="(isRight ? 'lang-right' : '') + ' ' + (isLeft ? 'lang-left' : '')">{{keywordsSet}}</cover-view>
</template>

<script>
	var app = getApp();
	import common from '../../utils/common.js'
	export default {
		name: 'lang',
		props: {
			left: {
				type: Boolean,
				default: true
			},
			right: {
				type: [Boolean, String],
				default: false
			},
			tag: {
				type: String,
				default: ''
			},
			keywords: {
				type: String,
				default: ''
			},
			X1: {
				type: [String, Number],
				default: ''
			},
			X2: {
				type: String,
				default: ''
			},
			unit: {
				type: String,
				default: ''
			},
			chineseTxt: {
				type: String,
				default: ''
			},
			needUpdate: {
				type:Boolean,
				value:false
			},
		},
		data() {
			return {
				key: '',
				keywordsSet: '',
				now_unit: '',
				xValue1: '',
				xValue2: '',
				isRight: false,
				isLeft: true
			}
		},
		watch: {
			X1() {
				if (this.chineseTxt) {
					return
				}
				var keywords = this.stitchKey(this.key);
				this.keywordsSet = keywords
				console.log('多语言更新', this.X1, this.key, keywords)
			}
		},
		created() {
			var _this = this;
			_this.isRight = _this.right
			_this.isLeft = _this.left
			this.key = this.keywords;
			var keywords = this.keywords;
			common.getConfig(function(data){
				if (!app) {
					app = getApp()
				}
				if (data.config.open_multilingual != 1 || (data.config.open_multilingual == 1 && data.config.lang_config.now_lang == 'chinese')) {
					_this.isLeft = false
					_this.isRight = false
					if (_this.chineseTxt) {
						_this.keywordsSet = _this.chineseTxt
					} else {
						keywords = _this.stitchKey(keywords);
						_this.keywordsSet = keywords
					}
				} else {
					common.getLangData(function(langData){
						if (typeof langData[keywords] == 'undefined') {
							common.reportLanguage(keywords);
							app.globalData.languageData[keywords] = keywords;
							keywords = _this.stitchKey(keywords);
							_this.keywordsSet = keywords
						} else if (langData[keywords] == '') {
							keywords = _this.stitchKey(keywords);
							_this.keywordsSet = keywords
						} else {
							_this.key = keywords;
							keywords = _this.stitchKey(langData[keywords]);
							_this.keywordsSet = keywords
						}
					})
				}
			})
		},
		methods: {
			stitchKey(key) {
				let X1 = this.X1
				let X2 = this.X2
				let langOptions = app.globalData.config.config
				if (this.unit) {
					this.now_unit = this.unit;

					if (this.unit == 'yuan') {
						this.now_unit = langOptions.Currency_txt
					}

					if (this.unit == 'yuanSymbol') {
						this.now_unit = langOptions.Currency_symbol
						X1 = this.now_unit + X1;
					} else if (!langOptions.lang_config || langOptions.lang_config.now_lang == 'chinese') {
						X1 += this.now_unit;
					} else {
						X1 += ' ' + this.now_unit;
					}
				}
				
				if (key.indexOf('X1') != -1) {
					key = key.replace('X1', X1);
				}
				
				if (key.indexOf('X2') != -1) {
					key = key.replace('X2', X2);
				}

				return key;
			}
		}
	}
</script>
<style scoped>
	.lang-box {
		display: inline-block;
	}

	.lang-wrapper {
		white-space: normal;
	}

	.laang-left {
		margin-left: 4px;
	}

	.lang-right {
		margin-right: 4px;
	}
</style>
